package com.example.foobar.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.example.foobar.dao.service.DaNewCommerCountDaoService;
import com.example.foobar.entity.DaNewCommerCount;
import com.example.foobar.mapper.DaNewCommerCountMapper;
import com.example.foobar.mapper.ImsPzGuanhuaiTrackMapper;
import com.example.foobar.param.DaNewCommerCountPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 新增用户数记录 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class DaNewCommerCountDaoServiceImpl extends BaseServiceImpl<DaNewCommerCountMapper, DaNewCommerCount> implements DaNewCommerCountDaoService {

    @Autowired
    private DaNewCommerCountMapper daNewCommerCountMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveDaNewCommerCount(DaNewCommerCount daNewCommerCount) throws Exception {
        return super.save(daNewCommerCount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateDaNewCommerCount(DaNewCommerCount daNewCommerCount) {
        return super.updateById(daNewCommerCount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteDaNewCommerCount(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public Paging<DaNewCommerCount> getDaNewCommerCountPageList(DaNewCommerCountPageParam daNewCommerCountPageParam) throws Exception {
        Page<DaNewCommerCount> page = new PageInfo<>(daNewCommerCountPageParam, OrderItem.desc(getLambdaColumn(DaNewCommerCount::getCreateTime)));
        LambdaQueryWrapper<DaNewCommerCount> wrapper = new LambdaQueryWrapper<>();
        IPage<DaNewCommerCount> iPage = daNewCommerCountMapper.selectPage(page, wrapper);
        return new Paging<DaNewCommerCount>(iPage);
    }

    @Override
    public int insert(DaNewCommerCount daNewCommerCount) {
        return daNewCommerCountMapper.insert(daNewCommerCount);
    }

    @Override
    public int deleteByWrapper(Wrapper<DaNewCommerCount> wrapper) {
        int delete = daNewCommerCountMapper.delete(wrapper);
        return delete;
    }

    @Override
    @Transactional
    public void insertIfNotExist(DaNewCommerCount daNewCommerCount) {

        LambdaQueryWrapper<DaNewCommerCount> lambdaQueryWrapper = new LambdaQueryWrapper<>(DaNewCommerCount.class)
                .eq(DaNewCommerCount::getUniacid, daNewCommerCount.getUniacid()).eq(DaNewCommerCount::getDate, daNewCommerCount.getDate());
        int count = daNewCommerCountMapper.selectCount(lambdaQueryWrapper);
        if(count == 0){
            daNewCommerCountMapper.insert(daNewCommerCount);
        }
    }

    @Override
    public List<DaNewCommerCount> getDaNewCommerByUniacidAndDate(Integer uniacid, Date baseDate) {
        LambdaQueryWrapper<DaNewCommerCount> wrapper = new LambdaQueryWrapper<>(DaNewCommerCount.class).eq(DaNewCommerCount::getUniacid, uniacid).eq(DaNewCommerCount::getDate, baseDate);
        List<DaNewCommerCount> daNewCommerCounts = daNewCommerCountMapper.selectList(wrapper);
        return daNewCommerCounts;
    }


}
